<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/18
 * Time: 14:43
 */

namespace common\models\Service;


use common\models\Cache;
use common\models\CommonFunction;
use common\models\DAO\DayAccrual;
use common\models\DAO\Product;
use common\models\DAO\ProductRate;
use common\models\DAO\TeamBrokerage;
use common\models\DAO\UserRate;
use common\models\DAO\UserWalletLog;

class DayService {

    /*
     * 获取日日宝信息
     */
    public static function get($id = 1){
        $cache_name = 'product_'.$id;
        $cache = Cache::get($cache_name);
        if(!$cache){
            $product = Product::findOne($id)->attributes;
            $info['id'] = $product['id'];
            $info['name'] = $product['name'];
            $info['rate'] = $product['rate'];
            $info['person'] = $product['person'];
            $info['money'] = $product['money'];
            $info['day'] = $product['day'];
            $info['info'] = $product['info'];
            Cache::set($cache_name, $info);
            $cache = $info;
        }
        return $cache;
    }

    /*
     * 获取所有产品信息
     */
    public static function getTotal(){
        $index = 1;
        $result = [];
        while($index <= 5){
            $cache = self::get($index);
            $cache['rate'] = CommonFunction::formatMoney($cache['rate']);
            switch ($index)
            {
                case 1:
                    $url = '/product.html';
                    break;
                case 2:
                    $url = '/product/month.html';
                    break;
                case 3:
                    $url = '/product/quarter.html';
                    break;
                case 4:
                    $url = '/product/semester.html';
                    break;
                default:
                    $url = '/product/year.html';
            }
            $cache['url'] = $url;
            $result[] = $cache;
            $index ++ ;
        }
        /*$model = Product::find()->all();
        $result['day'] = $model[0]->attributes;
        $result['day']['rate'] = CommonFunction::formatWan($result['day']['rate']);

        $result['month'] = $model[1]->attributes;
        $result['month']['rate'] = CommonFunction::formatWan($result['month']['rate']);

        $result['quarter'] = $model[2]->attributes;
        $result['quarter']['rate'] = CommonFunction::formatWan($result['quarter']['rate']);

        $result['year'] = $model[3]->attributes;
        $result['year']['rate'] = CommonFunction::formatWan($result['year']['rate']);*/
        return $result;

    }
    /**
     * 获取利率
     */
    public static function  getRate(){
        $info = self::get();
        $rate = $info['rate'];
        return $rate;
    }

    /*
     * 获得当前用户活期年化率
     */
    public static function getCurRate($userID){
        $now = date('Ymd');
        $rateInfo = UserRate::find()->where(['userID'=>$userID, 'status'=>1])->andWhere("$now<loseTime")->asArray()->one();
        $rate = 0;
        $rateInfo && $rate = $rate + intval($rateInfo['rate']);
        return $rate;
    }

    /*
    * 获得当前用户活期年化率
    */
    public static function getAppCurRate($userID){
        $now = date('Ymd');
        $rateInfo = UserRate::find()->where(['userID'=>$userID, 'status'=>1])->andWhere("$now<loseTime")->asArray()->one();
        $rate = ['rate'=>0, 'day'=>0];
        $rateInfo && $rate = ['rate'=> intval($rateInfo['rate']), 'day'=>intval((strtotime($rateInfo['loseTime']) - strtotime(date('Ymd')))/24/3600)];
        return $rate;
    }

    /*
     * 获取昨日的利率
     */
    public static function getLastRate(){
        $date = date('Ymd', strtotime('-1 day'));
        $rate = ProductRate::findOne(array('changeTime'=>$date));
        if($rate){
            $rate = $rate->attributes;
            $rate = $rate['rate'];
        }else{
            $rate = self::getRate();
        }
        return $rate;
    }

    /*
     * 计算昨日的利息
     */
    public static function getLastAccrual(){
        $date = date('Ymd', strtotime('-1 day'));
        $accrualModel = DayAccrual::find()->andWhere('typeID!=4')->andFilterWhere(['<', 'date', $date])->groupBy('userID')
            ->select('SUM(money) as money, userID')->having('money > 0')->asArray()
            ->all();
        return $accrualModel;
    }

    /*
     * 佣金体系
     */
    public static function brokerage($param){
        if(Wallet::remainder($param['userID']) < 1000)
            return false;

        $date = date('Ymd');
        $dayAccrual = new DayAccrual();
        $dayAccrual->userID = $param['userID'];
        $dayAccrual->date = $date;
        $dayAccrual->money = $param['money'];
        $dayAccrual->invest = $param['money'];
        $dayAccrual->createTime = $param['createTime'];
        $dayAccrual->updateTime = $param['createTime'];
        $dayAccrual->typeID = $param['typeID'];
        $dayAccrual->openUserID = $param['investUserID'];
        $dayAccrual->chargeID = 0;
        $accrual_result  = $dayAccrual->save();
        if(!$accrual_result){
            return false;
        }
        //钱包明细
        $user_wallet = new UserWalletLog();
        $user_wallet->type = 1;
        $user_wallet->money = $param['money'];
        $user_wallet->createTime = $param['createTime'];
        $user_wallet->userID = $param['userID'];
        $user_wallet->road = $param['typeID'];
        $user_wallet->desc = $param['desc'];
        $user_wallet_result = $user_wallet->save();
        if(!$user_wallet_result){
            return false;
        }
        if(!empty($param['investUserID'])){
            $team_brokerage = new TeamBrokerage();
            $team_brokerage->investID = 0;
            $team_brokerage->productID = 0;
            $team_brokerage->userID = $param['userID'];
            $team_brokerage->investUserID = $param['investUserID'];
            $team_brokerage->money = 0;
            $team_brokerage->brokerage = $param['money'];
            $team_brokerage->rate = 0;
            $team_brokerage->createTime = $param['createTime'];
            $team_brokerage->userInvestID = 0;
            $team_brokerage->userInvestID = 0;
            $brokerage_result = $team_brokerage->save();
            if(!$brokerage_result){
                return false;
            }
        }
        if(!empty($param['openID'])){
            SendWxMessage::charge($param['openID'], CommonFunction::formatMoney($param['money']), $param['desc'].'，您获得了'.CommonFunction::formatMoney($param['money']).'元佣金！');
        }
        return true;
    }

}